/**
 * @author Linrui
 * @since 2011-03-26
 * 用户的数据持久化封装类
 */
package com.qeado.user.dao.ibatis;

import org.springframework.stereotype.Repository;

import com.qeado.base.BaseDao;
import com.qeado.user.dao.UserDao;
import com.qeado.user.model.User;
import com.qeado.user.model.UserDaoParameterMap;

@Repository
@SuppressWarnings("unchecked")
public class UserDaoImplement extends BaseDao  implements UserDao{

	private UserDaoParameterMap parameterMap = new UserDaoParameterMap();

	public int checkPassword(String id, String password) {
		User user = new User();
		user.setId(id);
		user.setPassword(password);
		return ((Integer)this.getSqlMapClientTemplate().queryForObject("User.checkPassword", user)).intValue();
	}

	public boolean deleteUser(String userId) {
		try {
			this.getSqlMapClientTemplate().delete("User.deleteUser", userId);
			return true;
		} catch( Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	// 待实现
	public boolean insertMessage(String message) {
		return false;
	}

	public boolean insertUser(User user) {
		try {
			this.getSqlMapClientTemplate().insert("User.insertUser", user);
			return true;
		} catch (Exception e) {
			e.toString();
			return false;		
		}		
	}

	public User queryByNameByPassword(String userName, String password) {
		parameterMap.setParameter1(userName);
		parameterMap.setParameter2(password);
		return (User)this.getSqlMapClientTemplate().queryForObject("User.queryByNameByPassword",parameterMap);
	}

	public User queryById(String userId) {
		return (User)this.getSqlMapClientTemplate().queryForObject("User.queryById",userId);
	}

	public User queryByName(String userName) {
		return (User)this.getSqlMapClientTemplate().queryForObject("User.queryByName",userName);
	}

	public boolean updateBelongColleage(String colleageId, String userId) {
		parameterMap.setParameter1(colleageId);
		parameterMap.setParameter2(userId);
		try{ 
			this.getSqlMapClientTemplate().update("User.updateColleageId", parameterMap);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public boolean updateBelongCompany(String companyId, String userId) {
		parameterMap.setParameter1(companyId);
		parameterMap.setParameter2(userId);
		try{ 
			this.getSqlMapClientTemplate().update("User.updateCompanyId", parameterMap);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public boolean updatePassword(String ID, String password) {
		try{
			parameterMap.setParameter1(ID);
			parameterMap.setParameter2(password);	
			this.getSqlMapClientTemplate().update("User.updatePassword", parameterMap);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public boolean updateUser(User user) {
		try {
			this.getSqlMapClientTemplate().update("User.updateUser", user);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

}
